//
//  Validate.swift
//  TeaShop
//
//  Created by 刘泽 on 2018/1/16.
//  Copyright © 2018年 刘泽. All rights reserved.
//

import Foundation

public enum Validate {
    case email(_: String)
    case phoneNum(_: String)
    case carNum(_: String)
    case username(_: String)
    case password(_: String)
    case password8_(_: String)
    case account5_10(_: String)
    case nickname(_: String)
    case chineseName(_:String)
    case URL(_: String)
    case IP(_: String)
    case authCode(_: String)
    case phoneAuthCode(_: String)
    case IDCard(_:String)
    
    public var isRight: Bool {
        var predicateStr:String!
        var currObject:String!
        switch self {
        case let .email(str):
            predicateStr = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"
            currObject = str
        case let .phoneNum(str):
            predicateStr = "^1[345789]{1}[0-9]{9}$"
            currObject = str
        case let .carNum(str):
            predicateStr = "^[A-Za-z]{1}[A-Za-z_0-9]{5}$"
            currObject = str
        case let .username(str):
            predicateStr = "^[A-Za-z0-9]{6,20}+$"
            currObject = str
        case let .password(str):
            predicateStr = "^[a-zA-Z0-9]{6,16}+$"
            currObject = str
        case let .nickname(str):
            predicateStr = "^[\\u4e00-\\u9fa5]{4,8}$"
            currObject = str
        case let .URL(str):
            predicateStr = "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$"
            currObject = str
        case let .IP(str):
            predicateStr = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
            currObject = str
        case let .account5_10(str):
            //^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,10}$
            predicateStr = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{5,10}$"
            currObject = str
        case let .chineseName(str):
            predicateStr = "^[\\u4E00-\\u9FA5]{2,5}(?:·[\\u4E00-\\u9FA5]{2,5})*$"
            currObject = str
        case let .password8_(str):
            predicateStr = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,36}$"
            currObject = str
        case let .authCode(str):
            predicateStr = "^([0-9]){4}$"
            currObject = str
        case let .phoneAuthCode(str):
            predicateStr = "^([0-9]){6}$"
            currObject = str
        case let .IDCard(str):
            predicateStr = "^[1-9]d{5}(18|19|([23]d))d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)d{3}[0-9Xx]$"
            currObject = str
            
        }
        
        let predicate =  NSPredicate(format: "SELF MATCHES %@" ,predicateStr)
        return predicate.evaluate(with: currObject)
    }
}

